package com.yuma.tingshu.common.config;

import com.yuma.tingshu.common.constant.RabbitConstant;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//死信队列（用于关闭订单）
@Configuration
public class DelayDLConfig {

    /*@Bean
    public Exchange ttlExchange(){
        DirectExchange directExchange = new DirectExchange(RabbitConstant.ORDER_EXCHANGE);
        directExchange.setDelayed(true);
        return directExchange;
    }*/

    @Bean
    public Queue ttlQueue() {
        return QueueBuilder.durable(RabbitConstant.ORDER_TTL_QUEUE)
                .ttl(1000 * 30 * 60) // 为了延迟30秒
                .deadLetterRoutingKey(RabbitConstant.ORDER_DEAD_RK)
                .deadLetterExchange(RabbitConstant.ORDER_EXCHANGE)
                .build();
    }

    @Bean
    public Binding ttlBinding() {
        return new Binding(RabbitConstant.ORDER_TTL_QUEUE,
                Binding.DestinationType.QUEUE,
                RabbitConstant.ORDER_EXCHANGE,
                RabbitConstant.ORDER_TTL_RK,
                null);
    }

    @Bean
    public Queue dlQueue() {
        return QueueBuilder.durable(RabbitConstant.ORDER_DEAD_QUEUE).build();
    }

    @Bean
    public Binding dlBinding() {
        return new Binding(RabbitConstant.ORDER_DEAD_QUEUE,
                Binding.DestinationType.QUEUE,
                RabbitConstant.ORDER_EXCHANGE,
                RabbitConstant.ORDER_DEAD_RK,
                null);
    }

}
